package com.wy.redis;

import com.wy.model.sys.SystemValues;
import com.wy.utils.SerializeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
@Component
@Configuration
public class RedisCache {
    @Autowired
    private JedisPool jedisPool;
    @Value("spring.redis.cacheKey")
    private String key ;
    public  void setList(List<SystemValues> valuesList) {
        Jedis jedis = jedisPool.getResource();
            jedis.lpop(key);
            for(SystemValues values :valuesList){
                jedis.lpush(key.getBytes(), SerializeUtils.serialize(values));
            }
        jedisPool.returnResource(jedis);
    }
    public List<SystemValues> getList(){
        List<SystemValues> valuesList = new ArrayList<SystemValues>();
        Jedis jedis = jedisPool.getResource();
        List<byte[]> bytes = jedis.lrange(key.getBytes(),0,-1);
        for(byte[] bt : bytes){
            valuesList.add((SystemValues) SerializeUtils.unserialize(bt));
        }
        return valuesList;
    }
}
